package com.example.ai.model.data

import androidx.room.Entity
import androidx.room.PrimaryKey
import androidx.room.TypeConverter
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken

/**
 * Created by lijinxi on 2024/3/7.
 * @Description:
 */
@Entity(tableName = "messages")
data class MessageItem(
    @PrimaryKey val chatRoomId: String,
    val chatRoomTitle: String,
    val messages: MutableList<MessageData>
)

object Converters {
    @TypeConverter
    fun fromMessageList(messageList: MutableList<MessageData>): String {
        // 将MutableList<MessageData>转换为字符串，以便存储到数据库中
        return Gson().toJson(messageList)
    }

    @TypeConverter
    fun toMessageList(json: String): MutableList<MessageData> {
        // 从数据库中的字符串恢复MutableList<MessageData>
        val type = object : TypeToken<MutableList<MessageData>>() {}.type
        return Gson().fromJson(json, type)
    }
}